package com.tianhui.consignortwofei.mvp.ui.activity.hetong;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.fgs.common.CommonResponse;
import com.fgs.common.other.CommonEvent;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.dialog.OperateTipDialog;
import com.tianhui.consignortwofei.api.hetong.HuRequest;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.mvp.model.FuJianListBean;
import com.tianhui.consignortwofei.mvp.model.SendFuJianYuanBean;
import com.tianhui.consignortwofei.mvp.model.enty.OrderCondition;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.contract.ContractListContract;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.impl.HeTongIOrderStatus;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.presenter.FuJianListPresenter;
import com.tianhui.consignortwofei.mvp.ui.adapter.FuJianListAdapter;
import com.tianhui.consignortwofei.other.EventWhat;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemChildClickListener;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.List;

/**
 * @author fgs
 * @date 2020/8/27
 * @description
 **/
public class FuJianListFragment extends HeTongSearchFragment<ContractListContract.View, ContractListContract.Presenter> implements ContractListContract.View {

    private HeTongIOrderStatus mIOrderStatus;
    private String mSearchKey;
    private String mYinCangWitch="";

//    private ContractModel contractModel;
    private HuRequest huRequest;
    private DriverModel driverModel;

    public FuJianListFragment() {
        // Required empty public constructor
//        contractModel = new ContractModel();
        driverModel=new DriverModel();
    }

    @Override
    public boolean isNeedLazyLoad() {
        return false;
    }

    public static FuJianListFragment newInstance(HeTongIOrderStatus iOrderStatus) {
        FuJianListFragment orderListFragment = new FuJianListFragment();
        orderListFragment.setOrderStatus(iOrderStatus);

        return orderListFragment;
    }

    public FuJianListFragment setOrderStatus(HeTongIOrderStatus iOrderStatus) {
        mIOrderStatus = iOrderStatus;
        return this;
    }

    public FuJianListFragment setSearchKey(String searchKey) {
        mSearchKey = searchKey;
        return this;
    }

    public FuJianListFragment setWitch(String witch) {
        mYinCangWitch = witch;
        return this;
    }


    @Override
    protected ContractListContract.View createView() {
        huRequest = new HuRequest();
        return this;
    }

    @Override
    protected ContractListContract.Presenter createPresenter() {
        return new FuJianListPresenter(getContext());
    }


    @Override
    public RecyclerArrayAdapter createAdapter() {
//        return new ShenContractListAdapter(getContext(), mIOrderStatus,mYinCangWitch);
        return new FuJianListAdapter(getContext(),2);
    }

    @Override
    protected void initListener() {

        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
//                goQuotationDetail(position);
                List<FuJianListBean.RecordsDTO> list =mAdapter.getAllData();
                String code = list.get(position).getCode();
                deleteDiaLog(position,code);

            }
        });

        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                List<FuJianListBean.RecordsDTO> list =mAdapter.getAllData();
                String code = list.get(position).getCode();
                deleteDiaLog(position,code);
//                switch (view.getId()){
//                    case R.id.tv_xiugai:
//                        goQuotationDetail(position);
//                        break;
//                    case R.id.tv_shanchu:
//                        deleteDiaLog(position);
//
//                        break;
//                }
            }
        });

    }




    @Override
    public void requestDataList(int page, boolean isDialog) {
        if (mIOrderStatus != null) {
            OrderCondition orderCondition = new OrderCondition();
            orderCondition.page = page;
            orderCondition.searchKey = mSearchKey;
            getPresenter().requestOrderList(mIOrderStatus.getOrderListObservable(orderCondition), isDialog,huRequest,page);
        }
    }

    @Override
    public void initData() {


        fragment_contract_list_addContractButton.setVisibility(View.GONE);
        ll_search_body.setVisibility(View.GONE);
    }

    @Override
    protected void requestSearch(String searchKey) {
        mSearchKey = searchKey;
        refreshData(true);
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (EventWhat.valueOf(event.getWhat())) {
            case EVENT_REFRESH_UFJIAN_LIST:
                    refreshData(false);
                break;

        }
    }




    public void deleteDiaLog(int position,String code) {
        //填0改回未添加状态
        new OperateTipDialog.Builder(getActivity())
                .title("销售订单未上传附件合同原因")
                //                .content("存在多个牌号重量用逗号(,)隔开，调整数字即可，必须保留逗号。")
//                .input(fileName,new OperateTipDialog.InputCallback() {
//                    @Override
//                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
//
//                    }
//                })
                .input(new OperateTipDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        String reason = dialog.getInputContent();
                        if (TextUtils.isEmpty(reason)) {
                            ToastUtil.showShortToast("原因不能为空");
                            return;
                        }
                        dialog.dismiss();

                        deleteQuotation(position,reason,code);
                    }
                })
                .show();

    }




    private void deleteQuotation(int position,String reason,String code) {
        SendFuJianYuanBean jianYuanBean = new SendFuJianYuanBean();
        jianYuanBean.setSaleDeliveryCode(code);
        jianYuanBean.setReason(reason);

        driverModel.sendFuJianYuan(getContext(), jianYuanBean, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    ToastUtil.showShortToast("添加成功");
                    mAdapter.remove(position);
                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }






}
